// releasePage/pages/homePageInfo/ftScene/ftScene.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 18,
        index: 0,
        sceneContent: [],
        showCover: false,
        showEdit: false, //编辑显示
        inputValue: '',
        id: '', //场景id
        sceneDescription: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getFtNetWorkSceneList();
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 52,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        let that = this;
        that.setData({});
        that.getFtNetWorkSceneList();
    },
    //编辑模式
    showEdit(e) {
        let _that = this;
        if (app.powerIdentity('00,01')) return; //权限判断
        this.setData({
            showEdit: !this.data.showEdit,
        });
    },
    //显示添加网络场景
    showCover(e) {
        let _that = this;
        if (app.powerIdentity('00,01')) return; //权限判断
        _that.setData({
            showCover: true,
        });
    },
    getScene(e) {
        this.setData({
            inputValue: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 8);
    },
    sceneDescription(e) {
        this.setData({
            sceneDescription: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 40);
    },
    //选择网络场景
    chooseScene(e) {
        if (this.data.showEdit) {
            this.showCover();
            this.setData({
                inputValue: e.currentTarget.dataset.scene,
                index: e.currentTarget.dataset.index,
                id: e.currentTarget.dataset.id,
                sceneDescription: e.currentTarget.dataset.scenedesc,
            });
        } else {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
                ftScene: e.currentTarget.dataset.scene,
                ftSceneId: e.currentTarget.dataset.id,
            });
            wx.navigateBack({
                delta: 1, // 返回上一级页面。
            });
        }
    },
    //删除网络场景
    deleteScene(e) {
        let _that = this;
        if (_that.data.showEdit) {
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/countFtSpecialLocation?authId=${app.globalData.authId}&networksceneid=${e.currentTarget.dataset.id}`,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                },
                success: function (res) {
                    console.log(res.data.num);
                    if (res.data.num) {
                        wx.navigateTo({
                            url: `./deleteScene/deleteScene?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}&num=${res.data.num}`,
                        });
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '确认删除？',
                            success: function (res) {
                                if (res.confirm) {
                                    wx.request({
                                        url:
                                            app.globalData.rootPath +
                                            `cjMap/deleteFtNetWorkScene?authId=${app.globalData.authId}&id=${e.currentTarget.dataset.id}`,
                                        method: 'GET',
                                        header: {
                                            'content-type': 'application/json',
                                        },
                                        success: function (res) {
                                            console.log(res);
                                            _that.setData({
                                                sceneContent: [],
                                                inputValue: '',
                                            });
                                            let pages = getCurrentPages();
                                            let prevPage3 =
                                                pages[pages.length - 4]; //上上上一页面
                                            let prevPage2 =
                                                pages[pages.length - 3]; //上上一页面
                                            console.log(
                                                typeof prevPage2.refresh
                                            );
                                            if (
                                                typeof prevPage2.refresh ==
                                                'function'
                                            ) {
                                                prevPage2.refresh('ftPoint');
                                            }
                                            if (
                                                typeof prevPage3.refresh ==
                                                'function'
                                            ) {
                                                prevPage3.refresh('ftPoint');
                                            }
                                            _that.getFtNetWorkSceneList();
                                        },
                                    });
                                } else {
                                    console.log('点击取消回调');
                                }
                            },
                        });
                    }
                },
            });
        }
    },
    //弹窗
    //添加网络场景
    addScene(e) {
        let _that = this;
        if (!_that.data.inputValue) {
            wx.showToast({
                title: '请填写分类名称',
                icon: 'none',
                duration: 2000,
            });
            return;
        }
        if (_that.data.showEdit) {
            //编辑
            let arr = 'sceneContent[' + _that.data.index + '].networkscene';
            _that.setData({
                [arr]: _that.data.inputValue,
                showCover: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/editFtNetWorkScene?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    networkscene: _that.data.inputValue,
                    networkscenedesc: _that.data.sceneDescription,
                    id: _that.data.id,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        wx.showToast({
                            title: 'OK',
                            icon: 'success',
                            duration: 2000,
                        });
                    } else {
                        wx.showToast({
                            title: '提交失败，请重新录入或联系后台管理',
                            icon: 'none',
                            duration: 2000,
                        });
                    }
                    _that.setData({
                        sceneContent: [],
                        inputValue: '',
                    });
                    _that.getFtNetWorkSceneList();
                },
            });
        } else {
            //添加
            _that.setData({
                showCover: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/createFtNetWorkScene?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    networkscene: _that.data.inputValue,
                    networkscenedesc: _that.data.sceneDescription,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        wx.showToast({
                            title: 'OK',
                            icon: 'success',
                            duration: 2000,
                        });
                    } else {
                        wx.showToast({
                            title: '提交失败，请重新录入或联系后台管理',
                            icon: 'none',
                            duration: 2000,
                        });
                    }
                    _that.setData({
                        sceneContent: [],
                        inputValue: '',
                    });
                    _that.getFtNetWorkSceneList();
                },
            });
        }
    },
    hideCover(e) {
        this.setData({
            showCover: false,
        });
    },
    stopEvent(e) {
        return;
    },

    // 获取网络场景列表
    getFtNetWorkSceneList() {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getFtNetWorkSceneList?authId=${app.globalData.authId}&createtimeorder=asc`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res);
                _that.setData({
                    sceneContent: res.data.data,
                });
            },
        });
    },
});
